import pymysql
import unittest
class dbuilts(object):
    @classmethod
    def get_connect(cls):
        connect = pymysql.connect(host='',port='',user='',password='',database='',charset='')
        return connect
    @classmethod
    def query_one(cls,sql):
        connect = cls.get_connect()
        cursor = connect.cursor()
        cursor.execute(sql)
        one = cursor.fetchone()
        cursor.close()
        connect.cursor()
        return one
    @classmethod
    def query_all(cls,sql):
        connect = cls.get_connect()
        cursor = connect.cursor()
        cursor.execute(sql)
        _all = cursor.fetchall()
        cursor.close()
        connect.cursor()
        return _all
    @classmethod
    def iud_data(cls,sql):
        connect = cls.get_connect()
        cursor = connect.cursor()
        n = None
        try:
            n = cursor.execute(sql)
        except Exception as e:
            print('出现异常,执行回滚,异常为{}'.format(e))
            connect.rollback()
        else:
            print('没有出现异常,提交事务')
            connect.commit()
        finally:
            print('执行结束,关闭游标和断开连接')
            cursor.close()
            connect.close()
        return n
